#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));

    connect(ui->cbFont, SIGNAL(activated(QString)), this, SLOT(setFontFamily(QString)));
    connect(ui->btnMinus, SIGNAL(clicked()), this, SLOT(onSizeMinus()));
    connect(ui->btnPlus, SIGNAL(clicked()), this, SLOT(onSizePlus()));
    connect(ui->btnCodeP, SIGNAL(clicked()), this, SLOT(onCodeplus()));
    connect(ui->btnCodeM, SIGNAL(clicked()), this, SLOT(onCodeMinus()));
    connect(ui->editUcs2, SIGNAL(returnPressed()), this, SLOT(onCodeEnterUcs2()));
    connect(ui->editutf8, SIGNAL(returnPressed()), this, SLOT(onCodeEnterUtf8()));
    connect(ui->editStr, SIGNAL(returnPressed()), this, SLOT(onCodeEnterStr()));

    connect(ui->btnWidthP, SIGNAL(clicked()), this, SLOT(onBgWidthPlus()));
    connect(ui->btnWidthM, SIGNAL(clicked()), this, SLOT(onBgWidthMinus()));
    connect(ui->btnHeightP, SIGNAL(clicked()), this, SLOT(onBgHeightPlus()));
    connect(ui->btnHeightM, SIGNAL(clicked()), this, SLOT(onBgHeightMinus()));
    connect(ui->editPicW, SIGNAL(returnPressed()), this, SLOT(onPicWEnter()));
    connect(ui->editPicH, SIGNAL(returnPressed()), this, SLOT(onPicHEnter()));

    connect(ui->btnOutputAscii, SIGNAL(clicked()), this, SLOT(onCreateAscii()));
    connect(ui->btnOutputGBK, SIGNAL(clicked()), this, SLOT(onCreateGBK()));
    connect(ui->btnTest, SIGNAL(clicked()), this, SLOT(onTest()));
    connect(ui->btnOutputCJK, SIGNAL(clicked()), this, SLOT(onCreateCJK()));
    connect(ui->btnOutputGB2312, SIGNAL(clicked()), this, SLOT(onCreatGB2312()));
    connect(ui->btnOutputKana, SIGNAL(clicked()), this, SLOT(onCreateKana()));

//    connect(ui->cbSurround, SIGNAL(clicked()), this, SLOT(onCheckSurround()));
//    connect(ui->editSurround, SIGNAL(returnPressed()), this, SLOT(onEnterSurroundWidth()));
    connect(ui->cbWithWidth, SIGNAL(clicked()), this, SLOT(onCheckWithWidth()));
    connect(ui->cbOutputImg, SIGNAL(clicked()), this, SLOT(onCheckOutputImg()));

    connect(ui->btnXPlus, SIGNAL(clicked()), this, SLOT(onDxPlus()));
    connect(ui->btnXMinus, SIGNAL(clicked()), this, SLOT(onDxMinus()));
    connect(ui->btnYPlus, SIGNAL(clicked()), this, SLOT(onDyPlus()));
    connect(ui->btnYMinus, SIGNAL(clicked()), this, SLOT(onDyMinus()));

//    connect(ui->cmbbit, SIGNAL(activated(QString)), this, SLOT(onSelectBit(QString)));

    fontsize = 24;
    img_w = fontsize;
    img_h = fontsize;
    dx = 0;
    dy = 0;
    pixmap = new QPixmap(img_w, img_h);

    black = QColor(0,0,0);
//    pen.setColor(QColor(255,255,255));
//    pen_g.setColor(QColor(127,127,127));
//    pen_g2.setColor(QColor(63,63,63));
    pens = NULL;
    font.setFamily(QString::fromLocal8Bit("宋体"));
    font.setPixelSize(fontsize);
    changeFontSize();

    ucs2code = 12354;
    code[5] = 0;
    code[4] = 0;
    code[3] = 0;
    code[2] = 130;
    code[1] = 129;
    code[0] = 227;
//    char* code2 = "あ　";
//    code[0] = code2[0];
//    code[1] = code2[1];
//    code[2] = code2[2];
//    code[3] = code2[3];
//    code[4] = code2[4];
//    code[5] = code2[5];
//    QString c2 = QString::fromUtf8(code2);
//    drawChar(c2);
//    is_Surround = false;
//    stroke_width = 1;
    pens = new QPen[1];
    pens[0].setColor(QColor(255,255,255));
    is_output_width = false;
    is_output_img = false;
//    ui->cbSurround->setChecked(false);
//    ui->editStroke->setText("1");
//    ui->cmbbit->setCurrentIndex(0);
//    onSelectBit("1");
    bit_per_point = 1;
    is_creating = false;
    drawChar();

    ui->editPicW->setText(QString::number(img_w));
    ui->editPicH->setText(QString::number(img_h));
    ui->editUcs2->setText(QString::number(ucs2code, 16).toUpper());
    ui->editutf8->setText(QString::number(utf8code, 16).toUpper());
    ui->editStr->setText(QString::fromUtf8((const char*)code));
    ui->label->setText(QString::number(fontsize));

    ui->btnOutputGB2312->setEnabled(false);
    ui->cbWithWidth->setEnabled(true);
    ui->cbWithWidth->setChecked(true);
    is_output_width = true;
}

MainWindow::~MainWindow()
{
    delete ui;
}

// http://blog.csdn.net/mubingyun/article/details/4322402
//void MainWindow::onOutput(){
//    pixmap->toImage();
//}

void MainWindow::setFontFamily(const QString &f){
    font.setFamily(f);
    ui->editStr->setFont(font);
    drawChar();
}

//void MainWindow::onSelectBit(const QString &b){
//    int nb = b.toInt();
//    if (nb!=bit_per_point){
//        bit_per_point = nb;
//        delete[] pens;
//        switch (bit_per_point){
//        case 1:
//            pens = new QPen[1];
//            pens[0].setColor(QColor(255,255,255,255));
//            break;
//        case 2:
//            pens = new QPen[2];
//            pens[0].setColor(QColor(255,255,255,255));
//            pens[1].setColor(QColor(255,255,255,127));
//            break;
//        case 4:
//            pens = new QPen[2];
//            pens[0].setColor(QColor(255,255,255,255));
//            pens[1].setColor(QColor(255,255,255,17));
//            break;
//        }

//        drawChar();
//    }
//}

//void MainWindow::onCheckSurround(){
//    if (ui->cbSurround->isChecked()){
//        is_Surround = true;
//    }else{
//        is_Surround = false;
//    }
//    drawChar();
//}

//void MainWindow::onEnterSurroundWidth(){
//    int newsw = ui->editSurround->text().toInt();
//    newsw=newsw<5?newsw:4;
//    ui->editSurround->setText(QString::number(newsw));
//    if (newsw!=stroke_width){
//        stroke_width = newsw;
//        if (stroke_width==0){
//            bit_per_point = 1;
//            out_color_num = 2;
//        }else if (stroke_width==1){
//            bit_per_point = 2;
//            out_color_num = 4;
//        }else if (stroke_width==2){
//            bit_per_point = 4;
//            out_color_num = 16;
//        }else if (stroke_width==3){
//            bit_per_point = 4;
//            out_color_num = 16;
//        }else{
//            bit_per_point = 8;
//            out_color_num = 256;
//        }

//        delete[] pens;
//        int color_num = stroke_width+1;
//        pens = new QPen[color_num];
////        int d = 255/(color_num*color_num);
//        pens[0].setColor(QColor(255,255,255, 255));
//        for (int i=1;i<color_num; i++){
//            int c = (255-255*i/color_num)/color_num;
//            pens[i].setColor(QColor(255,255,255, c));
//        }
//        drawChar();
//    }
//}

void MainWindow::onCheckWithWidth(){
    if (ui->cbWithWidth->isChecked()){
        is_output_width = true;
    }else{
        is_output_width = false;
    }
}

void MainWindow::onCheckOutputImg(){
    if (ui->cbOutputImg->isChecked()){
        is_output_img = true;
    }else{
        is_output_img = false;
    }
}

int MainWindow::getOutputMode(){
    int mode = 0;
    if (is_mapping_table){
        mode |= 1;
    }
    if (is_output_width){
        mode |= 2;
    }
//    if (is_Surround){
//        mode |= 4;
//    }
//    mode |= bit_per_point<<8;
    return mode;
}

void MainWindow::onSizeMinus(){
    if (fontsize>8){
        fontsize-=1;
    }
    changeFontSize();

    drawChar();
}

void MainWindow::onSizePlus(){
    if (fontsize<80){
        fontsize+=1;
    }
    changeFontSize();

    drawChar();
}

void MainWindow::onDxPlus(){
//    if (dx<5){
        dx++;
        ui->lbDX->setText(QString::number(dx));
        drawChar();
//    }
}

void MainWindow::onDxMinus(){
//    if (dx>-5){
        dx--;
        ui->lbDX->setText(QString::number(dx));
        drawChar();
//    }
}

void MainWindow::onDyPlus(){
//    if (dy<5){
        dy++;
        ui->lbDY->setText(QString::number(dy));
        drawChar();
//    }
}

void MainWindow::onDyMinus(){
//    if (dy>-5){
        dy--;
        ui->lbDY->setText(QString::number(dy));
        drawChar();
//    }
}

void MainWindow::onCodeplus(){
    ucs2code++;
    ucs2ToUtf8();
    ui->editUcs2->setText(QString::number(ucs2code, 16).toUpper());
    ui->editStr->setText(QString::fromUtf8((const char*)code));
}

void MainWindow::onCodeMinus(){
    if (ucs2code>33){
        ucs2code--;
        ucs2ToUtf8();
        ui->editUcs2->setText(QString::number(ucs2code, 16).toUpper());
        ui->editStr->setText(QString::fromUtf8((const char*)code));
    }
}

void MainWindow::onCodeEnterUcs2(){
    bool ok;
    ucs2code = ui->editUcs2->text().toInt(&ok, 16);
    if (ok){
        ucs2ToUtf8();
    }
}

void MainWindow::onCodeEnterUtf8(){
    bool ok;
    utf8code = ui->editutf8->text().toLong(&ok, 16);
    long temp = utf8code;
    if (ok){
        uchar b[6];
        int n = 0;
//        uchar *p=b;
        uchar a = temp & 255;
        while (a!=0){
            b[n] = a;
            n++;
            temp=temp>>8;
            a = temp & 255;
        }
        uchar *p = code;
        while (n>0){
            *p = b[n-1];
            p++;
            n--;
        }
        *p = 0;

        drawChar();
        utf8ToUcs2();
    }
}

void MainWindow::onCodeEnterStr(){
    QString str = ui->editStr->text();
    QByteArray ba = str.toUtf8();
    uchar a = ba.at(0);
    if (a<0x80){
        code[0] = ba.at(0);
        code[1] = 0;
    }else if ((a & 0xF8) == 0xF0){
        code[0] = ba.at(0);
        code[1] = ba.at(1);
        code[2] = ba.at(2);
        code[3] = ba.at(3);
        code[4] = 0;
    }else if ((a & 0xF0) == 0xE0){
        code[0] = ba.at(0);
        code[1] = ba.at(1);
        code[2] = ba.at(2);
        code[3] = 0;
    }else if ((a & 0xE0) == 0xC0){
        code[0] = ba.at(0);
        code[1] = ba.at(1);
        code[2] = 0;
    }else{
        code[0] = 0;
    }
    code[5] = 0;
    utf8code = 0;
    uchar *p = code;
    while (*p!=0){
        utf8code = utf8code<<8;
        utf8code+=*p;
        p++;
        if ((*p & 0xC0) != 0x80){
            break;
        }
    }
    ui->editutf8->setText(QString::number(utf8code, 16).toUpper());
    utf8ToUcs2();
    drawChar();
}

void MainWindow::ucs2ToUtf8(){
    if (ucs2code<127){ // 1字节
        code[0]=ucs2code;
        code[1]=0;
    }else if (ucs2code<2047){ //2字节
        code[2] = 0;
        code[1] = (ucs2code&63) | 128;
        code[0] = ((ucs2code&(31<<6))>>6) | 192;
    }else if (ucs2code<65535){ //3字节
        code[3] = 0;
        code[2] = (ucs2code&63) | 128;
        code[1] = ((ucs2code&(63<<6))>>6) | 128;
        code[0] = ((ucs2code&(15<<12))>>12)|224;
    }else if (ucs2code<2097151){
        code[4] = 0;
        code[3] = (ucs2code&63) | 128;
        code[2] = ((ucs2code&(63<<6))>>6) | 128;
        code[1] = ((ucs2code&(63<<12))>>12) | 128;
        code[0] = ((ucs2code&(7<<18))>>18)|240;
    }
    utf8code = 0;
    uchar *p = code;
    while (*p!=0){
        utf8code = utf8code<<8;
        utf8code+=*p;
        p++;
    }
    ui->editutf8->setText(QString::number(utf8code, 16).toUpper());
    ui->editStr->setText(QString::fromUtf8((const char*)code));

    drawChar();
}

void MainWindow::utf8ToUcs2(){
    if (utf8code<0x80){
        ucs2code = utf8code;
    }else if (utf8code<0xdfc0){ // 11011111 11000000
        int c1 = utf8code & 63;
        int c2 = (utf8code & (31<<8))>>8;
        ucs2code = (c2<<6) | c1;
    }else if (utf8code<0xefbfc0){ // 11101111 10111111 11000000
        int c1 = utf8code & 63;
        int c2 = (utf8code & (63<<8))>>8;
        int c3 = (utf8code & (15<<16))>>16;
        ucs2code = (c3<<12)|(c2<<6)|c1;
    }else if (utf8code<0xf7fbfbfcL){ // 11110111 10111111 10111111 11000000
        int c1 = utf8code & 63;
        int c2 = (utf8code & (63<<8))>>8;
        int c3 = (utf8code & (63<<16))>>16;
        int c4 = (utf8code & (7<<24))>>24;
        ucs2code = (c4<<18)|(c3<<12)|(c2<<6)|c1;
    }
    ui->editUcs2->setText(QString::number(ucs2code, 16).toUpper());
    ui->editStr->setText(QString::fromUtf8((const char*)code));
}

void MainWindow::changeFontSize(){
    ui->label->setText(QString::number(fontsize));
//    ui->lbchar->resize(fontsize*2, fontsize*2);
}

void MainWindow::drawChar(){
//    delete pixmap;
//    pixmap = new QPixmap(fontsize+2, fontsize+2);
    QString c = QString::fromUtf8((const char*)code);
    QPainter painter(pixmap);
    font.setPixelSize(fontsize);
    painter.setFont(font);

//    QRect rect(0, 0, fontsize, fontsize);
//    QString c = QString::fromLocal8Bit("あ ");
    painter.fillRect(0, 0, pixmap->width(), pixmap->height(), black);


    int dw = img_w-dx;
    int dh = img_h-dy;
    int surround_width = 0;
    switch (bit_per_point){
    case 1:
        surround_width = 0;
        break;
    case 2:
        surround_width = 1;
        break;
    case 4:
        surround_width = 3;
    }


    if (surround_width!=0){
        dx=dx+surround_width;
        dy=dy+surround_width;

//        for (int i=0; i<surround_width; i++){
//            int j = i+1;
////            painter.setPen(pens[j]);

//            for (int x=dx-j; x<=dx+j; x++){
//                for (int y=dy-j; y<=dy+j; y++){
//                    if (abs(x-dx)+abs(y-dy) == j){
//                        painter.drawText(x, y, dw, dh, Qt::AlignLeft|Qt::AlignTop, c);
//                    }
//                }
//            }
//        }
        if (!is_creating){
            painter.setPen(pens[1]);
            for (int x=dx-surround_width; x<=dx+surround_width; x++){
                for (int y=dy-surround_width; y<=dy+surround_width; y++){
                    if (abs(x-dx)*abs(y-dy) <= surround_width*surround_width){
                        painter.drawText(x, y, dw, dh, Qt::AlignLeft|Qt::AlignTop, c);
                    }
                }
            }
        }
//        painter.setPen(pen_g2);
//        painter.drawText(dx-1, dy-1, dw, dh, Qt::AlignLeft|Qt::AlignTop, c);
//        painter.drawText(dx+1, dy-1, dw, dh, Qt::AlignLeft|Qt::AlignTop, c);
//        painter.drawText(dx-1, dy+1, dw, dh, Qt::AlignLeft|Qt::AlignTop, c);
//        painter.drawText(dx+1, dy+1, dw, dh, Qt::AlignLeft|Qt::AlignTop, c);

//        painter.setPen(pen_g);
//        painter.drawText(dx+0, dy-1, dw, dh, Qt::AlignLeft|Qt::AlignTop, c);
//        painter.drawText(dx-1, dy+0, dw, dh, Qt::AlignLeft|Qt::AlignTop, c);
//        painter.drawText(dx+1, dy+0, dw, dh, Qt::AlignLeft|Qt::AlignTop, c);
//        painter.drawText(dx+0, dy+1, dw, dh, Qt::AlignLeft|Qt::AlignTop, c);

        painter.setPen(pens[0]);
        painter.drawText(dx, dy, dw, dh, Qt::AlignLeft|Qt::AlignTop, c);
        dx=dx-surround_width;
        dy=dy-surround_width;
    }else{
        painter.setPen(pens[0]);
        painter.drawText(dx, dy, dw, dh, Qt::AlignLeft|Qt::AlignTop, c);
//        painter.drawText(dx+1, dy+1, dw, dh, Qt::AlignLeft|Qt::AlignTop, c);
    }
    painter.end();
    ui->lbchar->setPixmap(*pixmap);

}

void MainWindow::drawCharBone(){
    QString c = QString::fromUtf8((const char*)code);
    QPainter painter(pixmap);
    font.setPixelSize(fontsize);
    int dw = img_w-dx;
    int dh = img_h-dy;
    painter.setFont(font);
    painter.fillRect(0, 0, pixmap->width(), pixmap->height(), black);
    painter.setPen(pens[0]);
    painter.drawText(dx, dy, dw, dh, Qt::AlignLeft|Qt::AlignTop, c);
    painter.end();
}

//int MainWindow::getFontWidth(){

//}

void MainWindow::changeCanvasSize(){
    ui->editPicW->setText(QString::number(img_w));
    ui->editPicH->setText(QString::number(img_h));
    delete pixmap;
    pixmap = new QPixmap(img_w, img_h);

    drawChar();
}

void MainWindow::onBgWidthPlus(){
    if (img_w<80){
        img_w+=1;
        changeCanvasSize();
    }
}

void MainWindow::onBgWidthMinus(){
    if (img_w>8){
        img_w-=1;
        changeCanvasSize();
    }
}

void MainWindow::onBgHeightPlus(){
    if (img_h<80){
        img_h+=1;
        changeCanvasSize();
    }
}

void MainWindow::onBgHeightMinus(){
    if (img_h>8){
        img_h-=1;
        changeCanvasSize();
    }
}

void MainWindow::onCreateAscii(){
    QString curdir = QApplication::applicationDirPath();
    QFile filebin(curdir+"\\"+"asciifont.bin");
    filebin.open(QFile::WriteOnly);
    QDataStream fbin(&filebin);
    fbin.setByteOrder(QDataStream::LittleEndian);

    QFile filerange(curdir+"\\asciirange.bin");
    filerange.open(QFile::WriteOnly);
    QDataStream range(&filerange);
    range.setByteOrder(QDataStream::LittleEndian);

    QFile filetext(curdir+"\\asciitext.txt");
    filetext.open(QFile::WriteOnly|QFile::Text);
    QTextStream text(&filetext);

    is_mapping_table = false;
    int range_number = 1;
    range<<getOutputMode();
    range<<range_number;   // range数
    int realw;
    if (bit_per_point==1){
        realw = (img_w+7>>3)<<3;
    }else if (bit_per_point==2){
        realw = (img_w+3>>2)<<2;
    }else if (bit_per_point==4){
        realw = (img_w+1>>1)<<1;
    }else{
        realw = img_w;
    }
    range<<realw;
    range<<img_h;
    text<<"mode = "<<getOutputMode()<<endl;
    text<<"range number = "<<range_number<<endl;
    createFont(32, 95, fbin, range, text);

    filebin.close();
    filerange.close();
    filetext.close();
//    createFontTest("ascii", 32, 96);
}

//void MainWindow::onCreateAscii2(){
//    QString curdir = QApplication::applicationDirPath();
//    QFile filebin(curdir+"\\"+"FontAscii.bin");
//    filebin.open(QFile::WriteOnly);
//    QDataStream fbin(&filebin);
//    fbin.setByteOrder(QDataStream::LittleEndian);

//    QFile filerange(curdir+"\\asciirange.bin");
//    filerange.open(QFile::WriteOnly);
//    QDataStream range(&filerange);
//    range.setByteOrder(QDataStream::LittleEndian);

//    QFile filetext(curdir+"\\asciitext.txt");
//    filetext.open(QFile::WriteOnly|QFile::Text);
//    QTextStream text(&filetext);

//    is_mapping_table = false;
//    range<<getOutputMode();
//    range<<1;   // range数
//    int realw;
//    is_Surround = false;
//    is_output_width = false;
////    if (is_Surround){
////        realw = (img_w+3>>2)<<2;
////    }else{
//        realw = (img_w+7>>3)<<3;
////    }
//    range<<realw;
//    range<<img_h;
//    text<<"mode = "<<getOutputMode()<<endl;
//    text<<"range number = "<<1<<endl;
//    createFont(32, 95, fbin, range, text);

//    filebin.close();
//    filerange.close();
//    filetext.close();
////    createFontTest("ascii", 32, 96);
//}

void MainWindow::onTest(){
    QString curdir = QApplication::applicationDirPath();

    QFile filebin(curdir+"\\"+"test.bin");
    filebin.open(QFile::WriteOnly);
    QDataStream fbin(&filebin);
    fbin.setByteOrder(QDataStream::LittleEndian);

    QFile filerange(curdir+"\\testrange.bin");
    filerange.open(QFile::WriteOnly);
    QDataStream range(&filerange);
    range.setByteOrder(QDataStream::LittleEndian);

    QFile filetext(curdir+"\\testtext.txt");
    filetext.open(QFile::WriteOnly|QFile::Text);
    QTextStream text(&filetext);

    is_mapping_table = false;
    range<<getOutputMode();
    range<<1;
    int realw;
    if (bit_per_point==1){
        realw = (img_w+7>>3)<<3;
    }else if (bit_per_point==2){
        realw = (img_w+3>>2)<<2;
    }else if (bit_per_point==4){
        realw = (img_w+1>>1)<<1;
    }else{
        realw = img_w;
    }
    range<<realw;
    range<<img_h;
    text<<"mode = "<<getOutputMode()<<endl;
    text<<"range number = "<<3<<endl;

    createFont(ucs2code, 1, fbin, range, text);
//    createFont(0xf900, 302, fbin, range, text);
    // 27 中文竖排标点：FE10-FE1F （无
    // 28 CJK兼容符号（竖排变体、下划线、顿号）：FE30-FE4F
//    createFont(0xfe30, 32, fbin, range, text);
    // 4 全角ASCII、全角中英文标点、半宽片假名、半宽平假名、半宽韩文字母：FF00-FFEF
//    createFont(0xff00, 240, fbin, range, text);

    filebin.close();
    filerange.close();
    filetext.close();
//    createFontTest("test", ucs2code, 1);
}

// 封印
void MainWindow::onCreatGB2312(){
//    QString curdir = QApplication::applicationDirPath();

//    QFile filebin(curdir+"\\"+"gb2312font.bin");
//    filebin.open(QFile::WriteOnly);
//    QDataStream fbin(&filebin);
//    fbin.setByteOrder(QDataStream::LittleEndian);

//    QFile filerange(curdir+"\\gb2312range.bin");
//    filerange.open(QFile::WriteOnly);
//    QDataStream range(&filerange);
//    range.setByteOrder(QDataStream::LittleEndian);

//    QFile filetext(curdir+"\\gb2312text.txt");
//    filetext.open(QFile::WriteOnly|QFile::Text);
//    QTextStream text(&filetext);
//    for (int i=0xB0; i<0xF7; i++){
//        // A1~FE
//        createFont((i<<8)|0xA1, 94, fbin, range, text);
//    }
//    filebin.close();
//    filerange.close();
//    filetext.close();
}

void MainWindow::onCreateGBK(){
    QString curdir = QApplication::applicationDirPath();

    QFile filebin(curdir+"\\"+"gbkfont.bin");
    filebin.open(QFile::WriteOnly);
    QDataStream fbin(&filebin);
    fbin.setByteOrder(QDataStream::LittleEndian);

    QFile filerange(curdir+"\\gbkrange.bin");
    filerange.open(QFile::WriteOnly);
    QDataStream range(&filerange);
    range.setByteOrder(QDataStream::LittleEndian);

    QFile filetext(curdir+"\\gbktext.txt");
    filetext.open(QFile::WriteOnly|QFile::Text);
    QTextStream text(&filetext);

    is_mapping_table = false;
    range<<getOutputMode();
    range<<126; //range数
    int realw;
    if (bit_per_point==1){
        realw = (img_w+7>>3)<<3;
    }else if (bit_per_point==2){
        realw = (img_w+3>>2)<<2;
    }else if (bit_per_point==4){
        realw = (img_w+1>>1)<<1;
    }else{
        realw = img_w;
    }
    range<<realw;
    range<<img_h;
    text<<"mode = "<<getOutputMode()<<endl;
    text<<"range number = "<<126<<endl;

    for (int i=0x81; i<0xFF; i++){
        // 40~FE
        createFont((i<<8)|0x40, 191, fbin, range, text);
    }
    filebin.close();
    filerange.close();
    filetext.close();
}

/**
1）标准CJK文字

http://www.unicode.org/Public/UNIDATA/Unihan.html

Code point range	Block name	Release
U+3400..U+4DB5	CJK Unified Ideographs Extension A	3.0
U+4E00..U+9FA5	CJK Unified Ideographs	1.1
U+9FA6..U+9FBB	CJK Unified Ideographs	4.1
U+F900..U+FA2D	CJK Compatibility Ideographs	1.1
U+FA30..U+FA6A	CJK Compatibility Ideographs	3.2     (无
U+FA70..U+FAD9	CJK Compatibility Ideographs	4.1     (无
U+20000..U+2A6D6	CJK Unified Ideographs Extension B	3.1 (无
U+2F800..U+2FA1D	CJK Compatibility Supplement	3.1  (无
2）全角ASCII、全角中英文标点、半宽片假名、半宽平假名、半宽韩文字母：FF00-FFEF
http://www.unicode.org/charts/PDF/UFF00.pdf
3）CJK部首补充：2E80-2EFF
http://www.unicode.org/charts/PDF/U2E80.pdf
4）CJK标点符号：3000-303F
http://www.unicode.org/charts/PDF/U3000.pdf
5）CJK笔划：31C0-31EF
http://www.unicode.org/charts/PDF/U31C0.pdf
6）康熙部首：2F00-2FDF
http://www.unicode.org/charts/PDF/U2F00.pdf
7）汉字结构描述字符：2FF0-2FFF
http://www.unicode.org/charts/PDF/U2FF0.pdf
8）注音符号：3100-312F
http://www.unicode.org/charts/PDF/U3100.pdf
9）注音符号（闽南语、客家语扩展）：31A0-31BF
http://www.unicode.org/charts/PDF/U31A0.pdf
10）日文平假名：3040-309F
http://www.unicode.org/charts/PDF/U3040.pdf
11）日文片假名：30A0-30FF
http://www.unicode.org/charts/PDF/U30A0.pdf
12）日文片假名拼音扩展：31F0-31FF
http://www.unicode.org/charts/PDF/U31F0.pdf
13）韩文拼音：AC00-D7AF
http://www.unicode.org/charts/PDF/UAC00.pdf
14）韩文字母：1100-11FF
http://www.unicode.org/charts/PDF/U1100.pdf
15）韩文兼容字母：3130-318F
http://www.unicode.org/charts/PDF/U3130.pdf
16）太玄经符号：1D300-1D35F
http://www.unicode.org/charts/PDF/U1D300.pdf
17）易经六十四卦象：4DC0-4DFF
http://www.unicode.org/charts/PDF/U4DC0.pdf
18）彝文音节：A000-A48F
http://www.unicode.org/charts/PDF/UA000.pdf
19）彝文部首：A490-A4CF
http://www.unicode.org/charts/PDF/UA490.pdf
20）盲文符号：2800-28FF
http://www.unicode.org/charts/PDF/U2800.pdf
21）CJK字母及月份：3200-32FF
http://www.unicode.org/charts/PDF/U3200.pdf
22）CJK特殊符号（日期合并）：3300-33FF
http://www.unicode.org/charts/PDF/U3300.pdf
23）装饰符号（非CJK专用）：2700-27BF
http://www.unicode.org/charts/PDF/U2700.pdf
24）杂项符号（非CJK专用）：2600-26FF
http://www.unicode.org/charts/PDF/U2600.pdf
25）中文竖排标点：FE10-FE1F
http://www.unicode.org/charts/PDF/UFE10.pdf
26）CJK兼容符号（竖排变体、下划线、顿号）：FE30-FE4F
http://www.unicode.org/charts/PDF/UFE30.pdf
*/
void MainWindow::onCreateCJK(){
    QString curdir = QApplication::applicationDirPath();

    QFile filebin(curdir+"\\"+"cjkfont.bin");
    filebin.open(QFile::WriteOnly);
    QDataStream fbin(&filebin);
    fbin.setByteOrder(QDataStream::LittleEndian);

    QFile filerange(curdir+"\\cjkrange.bin");
    filerange.open(QFile::WriteOnly);
    QDataStream range(&filerange);
    range.setByteOrder(QDataStream::LittleEndian);

    QFile filetext(curdir+"\\cjktext.txt");
    filetext.open(QFile::WriteOnly|QFile::Text);
    QTextStream text(&filetext);

    is_mapping_table = false;
    int range_number = 11;
    range<<getOutputMode();
    range<<range_number;
    int realw;
    if (bit_per_point==1){
        realw = (img_w+7>>3)<<3;
    }else if (bit_per_point==2){
        realw = (img_w+3>>2)<<2;
    }else if (bit_per_point==4){
        realw = (img_w+1>>1)<<1;
    }else{
        realw = img_w;
    }
    range<<realw;
    range<<img_h;
    text<<"mode = "<<getOutputMode()<<endl;
    text<<"range number = "<<range_number<<endl;

    createFont(0x00A1, 95, fbin, range, text);
    // General punctuation (通用标点)
    createFont(0x2000, 0x6f, fbin, range, text);
    // 16 韩文字母：1100-11FF (取消
//    createFont(0x1100, 256, fbin, range, text);
    // 26 杂项符号（非CJK专用）：2600-26FF (实际只到266f
    createFont(0x2600, 112, fbin, range, text);
    // 25 装饰符号（非CJK专用）：2700-27BF
    createFont(0x2700, 192, fbin, range, text);
    // 22 盲文符号：2800-28FF    (无
//    createFont(0x2800, 256, fbin, range, text);
    // 5 CJK部首补充：2E80-2EFF
    // 8 康熙部首：2F00-2FDF
//    createFont(0x2e80, 352, fbin, range, text); //取消
    // 9 汉字结构描述字符：2FF0-2FFF     (取消
//    createFont(0x2ff0, 16, fbin, range, text);
    // 6 CJK标点符号：3000-303F
    // 12 日文平假名：3040-309F
    // 13 日文片假名：30A0-30FF
    createFont(0x3000, 256, fbin, range, text);
    // 10 注音符号：3100-312F (取消
    // 17 韩文兼容字母：3130-318F  (取消
    // 11 注音符号（闽南语、客家语扩展）：31A0-31BF （取消
    // 7 CJK笔划：31C0-31EF （无
    // 14 日文片假名拼音扩展：31F0-31FF （无
    // 23 CJK字母及月份：3200-32FF
    // 24 CJK特殊符号（日期合并）：3300-33FF
    createFont(0x3200, 512, fbin, range, text);
    // 1 U+3400..U+4DB5	CJK Unified Ideographs Extension A
    createFont(0x3400, 6582, fbin, range, text);
    // 19 易经六十四卦象：4DC0-4DFF
    // 2 U+4E00..U+9FBB	CJK Unified Ideographs
    createFont(0x4e00, 20924, fbin, range, text);
    // 20 彝文音节：A000-A48F    （无
    // 21 彝文部首：A490-A4CF    （无
    // 15 韩文拼音：AC00-D7AF    （取消
    // 3 U+F900..U+FA2D	CJK Compatibility Ideographs
    createFont(0xf900, 302, fbin, range, text);
    // 27 中文竖排标点：FE10-FE1F （无
    // 28 CJK兼容符号（竖排变体、下划线、顿号）：FE30-FE4F
    createFont(0xfe30, 32, fbin, range, text);
    // 4 全角ASCII、全角中英文标点、半宽片假名、半宽平假名、半宽韩文字母：FF00-FFEF
    createFont(0xff00, 240, fbin, range, text);
    // 18 太玄经符号：1D300-1D35F (无
    filebin.close();
    filerange.close();
    filetext.close();
}

/**
  * 只输出假名
  */
void MainWindow::onCreateKana(){
    QString curdir = QApplication::applicationDirPath();

    QFile filebin(curdir+"\\"+"kanafont.bin");
    filebin.open(QFile::WriteOnly);
    QDataStream fbin(&filebin);
    fbin.setByteOrder(QDataStream::LittleEndian);

    QFile filerange(curdir+"\\kanarange.bin");
    filerange.open(QFile::WriteOnly);
    QDataStream range(&filerange);
    range.setByteOrder(QDataStream::LittleEndian);

    QFile filetext(curdir+"\\kanatext.txt");
    filetext.open(QFile::WriteOnly|QFile::Text);
    QTextStream text(&filetext);

    is_mapping_table = false;
    int range_number = 2;
    range<<getOutputMode();
    range<<range_number;
    int realw;
    if (bit_per_point==1){
        realw = (img_w+7>>3)<<3;
    }else if (bit_per_point==2){
        realw = (img_w+3>>2)<<2;
    }else if (bit_per_point==4){
        realw = (img_w+1>>1)<<1;
    }else{
        realw = img_w;
    }
    range<<realw;
    range<<img_h;
    text<<"mode = "<<getOutputMode()<<endl;
    text<<"range number = "<<range_number<<endl;

    // 12 日文平假名：3040-309F
    // 13 日文片假名：30A0-30FF
    createFont(0x3040, 192, fbin, range, text);
    // 14 日文片假名拼音扩展：31F0-31FF （无
    createFont(0x31F0, 16, fbin, range, text);

    filebin.close();
    filerange.close();
    filetext.close();
}

void MainWindow::createFont(int start, int count,
                            QDataStream &fbin, QDataStream &range, QTextStream &text){
    int ct;
    int rcount;
    if (count>128){
        rcount = 128;
    }else{
        rcount = count;
    }

    int remain = count-rcount;
    int realw;

//    if (is_Surround){
//        realw = (img_w+3>>2)<<2;
//    }else{
//        realw = (img_w+7>>3)<<3;
//    }
    if (bit_per_point==1){
        realw = (img_w+7>>3)<<3;
    }else if (bit_per_point==2){
        realw = (img_w+3>>2)<<2;
    }else if (bit_per_point==4){
        realw = (img_w+1>>1)<<1;
    }else{
        realw = img_w;
    }
    ucs2code = start;
    ucs2ToUtf8();
    range<<start;
    range<<count;
    QImage buff(realw, img_h*rcount, QImage::Format_ARGB32_Premultiplied);
    QPainter painter(&buff);
    while(rcount>0){
        ct = rcount;
        painter.fillRect(0, 0, buff.width(), buff.height(), black);
        int out_size = 0;
//        if (is_Surround){
//        if (surround_width>0){
//            out_size = realw*img_h*rcount>>2;
//        }else{
//            out_size = realw*img_h*rcount>>3;
//        }
        out_size = realw*img_h*rcount*bit_per_point>>3;

        if (is_output_width){
            out_size+=rcount*4;
        }
        uchar *out = new uchar[out_size];
//        ucs2code = start;
        int ty = 0;
        while(ct>0){
            ucs2ToUtf8();
            ucs2code++;
            QImage w = pixmap->toImage();
            painter.drawImage(0, ty, w, 0, 0);
            ty+=img_h;
            ct--;
        }
        QImage buff2 = buff.convertToFormat(QImage::Format_RGB888, Qt::AutoColor);
        QString curdir = QApplication::applicationDirPath();
        if (is_output_img){
            buff2.save(curdir+"\\"+ QString::number(ucs2code-rcount, 16).toUpper()
                       +"~"
                       +QString::number(ucs2code, 16).toUpper() +".png");
        }
        const uchar* data = buff2.constBits();
        const uchar* p=data;
        uchar* p2=out;
        for (int k=0; k<rcount; k++){
            int *cwidth = (int*)p2;
            if (is_output_width){
                p2+=4;
            }
            int w = 0;
            for (int i = 0; i<img_h; i++){
//                if (is_Surround){
//                if (surround_width>0){
//                    for (int j=0; j<realw; j+=4){
//                        uchar p3[4];
//                        for (int z=0; z<4; z++){
//                            if (*p!=0 && w<j+z+1){
//                                w = j+z+1;
//                            }

//                            switch (*p){
//                            case 0:
//                                p3[z]=0;
//                                break;
//                            case 63:
//                                p3[z]=3;
//                                break;
//                            case 127:
//                                p3[z]=2;
//                                break;
//                            case 255:
//                                p3[z]=1;
//                                break;
//                            }
//                            p+=3;
//                        }
//                        *p2 = p3[0]|(p3[1]<<2)|(p3[2]<<4)|(p3[3]<<6);
//                        p2++;
//                    }
//                }else{
//                    for (int j=0; j<realw; j+=8){
//                        uchar p3[8];
//                        for (int z=0; z<8; z++){
//                            if (*p!=0){
//                                p3[z]=1;
//                                if (w<j+z+1){
//                                    w = j+z+1;
//                                }
//                            }else{
//                                p3[z]=0;
//                            }
//                            p+=3;
//                        }
//                        *p2 = p3[0]|(p3[1]<<1)|(p3[2]<<2)|(p3[3]<<3)
//                                |(p3[4]<<4)|(p3[5]<<5)|(p3[6]<<6)|(p3[7]<<7);
//                        p2++;
//                    }
//                }
                if (bit_per_point==1){
                    for (int j=0; j<realw; j+=8){
                        uchar p3[8];
                        for (int z=0; z<8; z++){
                            if (*p!=0){
                                p3[z]=1;

                                if (w<j+z+1){
                                    w = j+z+1;
                                }
                            }else{
                                p3[z]=0;
                            }
                            p+=3;
                        }
                        *p2 = p3[0]|(p3[1]<<1)|(p3[2]<<2)|(p3[3]<<3)
                                |(p3[4]<<4)|(p3[5]<<5)|(p3[6]<<6)|(p3[7]<<7);
                        p2++;
                    }
                }else if (bit_per_point==2){
                    for (int j=0; j<realw; j+=4){
                        uchar p3[4];
                        for (int z=0; z<4; z++){
                            if (*p!=0){
                                if (w<j+z+1){
                                    w = j+z+1;
                                }
                                if (*p==255){
                                    p3[z]=3;
                                }else if (*p>64){
                                    p3[z]=2;
                                }else{
                                    p3[z]=1;
                                }
                            }else{
                                p3[z]=0;
                            }
                            p+=3;
                        }
                        *p2 = p3[0]|(p3[1]<<2)|(p3[2]<<4)|(p3[3]<<6);
                        p2++;
                    }
                }else if (bit_per_point==4){
                    for (int j=0; j<realw; j+=2){
                        uchar p3[2];
                        for (int z=0; z<2; z++){
                            if (*p!=0){
                                if (w<j+z+1){
                                    w = j+z+1;
                                }
                                p3[z]=*p>>4;
                                if(p3[z]==0){
                                    p3[z] = 1;
                                }
                            }else{
                                p3[z]=0;
                            }
                            p+=3;
                        }
                        *p2 = p3[0]|(p3[1]<<4);
                        p2++;
                    }
                }else{
                    for (int j=0; j<realw; j++){
                        *p2 = *p;
                        p+=3;
                        p2++;
                    }
                }
            }
            if (is_output_width){
                if (w == 0){
                    w = img_w;
                }
                *cwidth = w;
            }
        }

        fbin.writeRawData((const char*)out, out_size);
        delete[] out;
        if (remain>128){
            remain = remain-128;
            rcount = 128;
        }else{
            rcount = remain;
            remain = 0;
        }
    }
    text<<"start = "<<start<<", ";
    text<<"count = "<<count<<endl;

}

//void MainWindow::createFontTest(const QString &name, int start, int count){
//    int ct = count;
//    int realw = (img_w+3>>2)<<2;
//    QImage buff(realw, img_h*count, QImage::Format_ARGB32_Premultiplied);
//    QPainter painter(&buff);
//    painter.fillRect(0, 0, realw, img_h*count, black);
//    uchar out[realw*img_h*count>>2];
//    ucs2code = start;
//    int ty = 0;
//    while(ct>0){
//        ucs2ToUtf8();
//        ucs2code++;
//        QImage w = pixmap->toImage();
//        painter.drawImage(0, ty, w, 0, 0);
//        ty+=img_h;
//        ct--;
//    }
//    QImage buff2 = buff.convertToFormat(QImage::Format_RGB888, Qt::AutoColor);
//    QString curdir = QApplication::applicationDirPath();
//    buff2.save(curdir+"\\"+ name +".png");

//    const uchar* data = buff2.constBits();
//    const uchar* p=data;
//    uchar* p2=out;
//    for (int k = 0; k<count; k++){
//        for (int i = 0; i<img_h*count; i++){
//            for (int j=0; j<realw; j+=4){
//                uchar p3[4];
//                for (int z=0; z<4; z++){
//                    switch (*p){
//                    case 0:
//                        p3[z]=0;
//                        break;
//                    case 63:
//                        p3[z]=3;
//                        break;
//                    case 127:
//                        p3[z]=2;
//                        break;
//                    case 255:
//                        p3[z]=1;
//                        break;
//                    }
//                    p+=3;
//                }
//                *p2 = p3[0]|(p3[1]<<2)|(p3[2]<<4)|(p3[3]<<6);
//                p2++;
//            }
//        }
//    }
//    QFile filebin(curdir+"\\"+ name+".bin");
//    filebin.open(QFile::WriteOnly);
//    QDataStream dsbin(&filebin);
//    dsbin.setByteOrder(QDataStream::LittleEndian);
//    /**
//     * 字符宽
//     * 字符高
//     * 编码始
//     * 字符数量
//     */
//    dsbin<<realw;
//    dsbin<<img_h;
//    dsbin<<start;
//    dsbin<<count;
//    dsbin.writeRawData((const char*)out, realw*img_h*count>>2);
//    filebin.close();

//    QFile filerange(curdir+"\\"+name+"range.bin");
//    filerange.open(QFile::WriteOnly);
//    QDataStream range(&filerange);
//    range<<start;
//    range<<count;

//}


void MainWindow::onPicWEnter(){
    bool ok;
    img_w = ui->editPicW->text().toInt(&ok, 10);
    if (ok){
        changeCanvasSize();
    }
}

void MainWindow::onPicHEnter(){
    bool ok;
    img_h = ui->editPicH->text().toInt(&ok, 10);
    if (ok){
        changeCanvasSize();
    }
}

void MainWindow::createFont2(int start, int count,
                             QDataStream &fbin, QDataStream &range, QTextStream &text){
    int ct;
    int rcount;
    if (count>128){
        rcount = 128;
    }else{
        rcount = count;
    }

    int remain = count-rcount;
    int realw;

    if (bit_per_point==1){
        realw = (img_w+7>>3)<<3;
    }else if (bit_per_point==2){
        realw = (img_w+3>>2)<<2;
    }else if (bit_per_point==4){
        realw = (img_w+1>>1)<<1;
    }else{
        realw = img_w;
    }

    is_creating = true;
    ucs2code = start;
    ucs2ToUtf8();
    range<<start;
    range<<count;
    QImage buff(realw, img_h*rcount, QImage::Format_ARGB32_Premultiplied);
    QPainter painter(&buff);
    while(rcount>0){
        ct = rcount;
        painter.fillRect(0, 0, buff.width(), buff.height(), black);
        int out_size = 0;
        out_size = realw*img_h*rcount*bit_per_point>>3;

        if (is_output_width){
            out_size+=rcount*4;
        }
        uchar *out = new uchar[out_size];
//        ucs2code = start;
        int ty = 0;
        while(ct>0){
            ucs2ToUtf8();
            ucs2code++;
            QImage w = pixmap->toImage();
            painter.drawImage(0, ty, w, 0, 0);
            ty+=img_h;
            ct--;
        }
        QImage buff2 = buff.convertToFormat(QImage::Format_RGB888, Qt::AutoColor);
        QString curdir = QApplication::applicationDirPath();
        if (is_output_img){
            buff2.save(curdir+"\\"+ QString::number(ucs2code-rcount, 16).toUpper()
                       +"~"
                       +QString::number(ucs2code, 16).toUpper() +".png");
        }
        const uchar* data = buff2.constBits();
        uchar *data2 = new uchar[realw*img_h*rcount];
        memset(data2, 0, realw*img_h*rcount);
        const uchar* p=data;
        switch (bit_per_point){
        case 1:
            for (int i=0; i<img_h*rcount; i++){
                for (int j=0; j<realw; j++){
                    if (data[(i*realw+j)*3]!=0){
                        data2[i*realw+j]=1;
                    }
                }
            }
        break;
        case 2:
            for (int i=0; i<img_h*rcount; i++){
                for (int j=0; j<realw; j++){
                    if (data[(i*realw+j)*3]!=0){
                        if (data2[i*realw+j]<3){
                            data2[i*realw+j]+=1;
                        }
                        if (data2[(i-1)*realw+j]<2){
                            data2[(i-1)*realw+j]+=1;
                        }
                        if (data2[(i+1)*realw+j]<2){
                            data2[(i+1)*realw+j]+=1;
                        }
                        if (data2[i*realw+j-1]<2){
                            data2[i*realw+j-1]+=1;
                        }
                        if (data2[i*realw+j+1]<2){
                            data2[i*realw+j+1]+=1;
                        }
                    }
                }
            }
            break;
        case 4:
            for (int i=0; i<img_h*rcount; i++){
                for (int j=0; j<realw; j++){
                    if (data[(i*realw+j)*3]!=0){
                        if (data2[i*realw+j]<13){
                            data2[i*realw+j]+=3;
                        }else{
                            data2[i*realw+j]=15;
                        }

                        if (data2[(i-1)*realw+j]<12){
                            data2[(i-1)*realw+j]+=2;
                        }else{
                            data2[(i-1)*realw+j]=13;
                        }
                        if (data2[(i+1)*realw+j]<12){
                            data2[(i+1)*realw+j]+=2;
                        }else{
                            data2[(i+1)*realw+j]=13;
                        }
                        if (data2[i*realw+j-1]<12){
                            data2[i*realw+j-1]+=2;
                        }else{
                            data2[i*realw+j-1]=13;
                        }
                        if (data2[i*realw+j+1]<12){
                            data2[i*realw+j+1]+=2;
                        }else{
                            data2[i*realw+j+1]=13;
                        }

                        if (data2[(i-2)*realw+j]<11){
                            data2[(i-2)*realw+j]+=1;
                        }
                        if (data2[(i-1)*realw+j+1]<11){
                            data2[(i-1)*realw+j+1]+=1;
                        }
                        if (data2[i*realw+j+2]<11){
                            data2[i*realw+j+2]+=1;
                        }
                        if (data2[(i+1)*realw+j+1]<11){
                            data2[(i+1)*realw+j+1]+=1;
                        }
                        if (data2[(i+2)*realw+j]<11){
                            data2[(i+2)*realw+j]+=1;
                        }
                        if (data2[(i+1)*realw+j-1]<11){
                            data2[(i+1)*realw+j-1]+=1;
                        }
                        if (data2[i*realw+j-2]<11){
                            data2[i*realw+j-2]+=1;
                        }
                        if (data2[(i-1)*realw+j-1]<11){
                            data2[(i-1)*realw+j-1]+=1;
                        }
                    }
                }
            }
            break;
        }

        uchar* p1=data2;
        uchar* p2=out;
        for (int k=0; k<rcount; k++){
            int *cwidth = (int*)p2;
            if (is_output_width){
                p2+=4;
            }
            int w = 0;

            switch (bit_per_point){
            case 1:
                for (int i = 0; i<img_h; i++){
                    for (int j=0; j<realw; j+=8){
                        for (int z=0; z<8; z++){
                            if (p1[z]!=0){
                                if (w<j+z+1){
                                    w = j+z+1;
                                }
                            }
                        }
                        *p2 = p1[0]|(p1[1]<<1)|(p1[2]<<2)|(p1[3]<<3)
                                |(p1[4]<<4)|(p1[5]<<5)|(p1[6]<<6)|(p1[7]<<7);
                        p2++;
                        p1+=8;
                    }
                }
                break;
            case 2:
                for (int i = 0; i<img_h; i++){
                    for (int j=0; j<realw; j+=4){
                        for (int z=0; z<4; z++){
                            if (p1[z]!=0){
                                if (w<j+z+1){
                                    w = j+z+1;
                                }
                            }
                        }
                        *p2 = p1[0]|(p1[1]<<2)|(p1[2]<<4)|(p1[3]<<6);
                        p2++;
                        p1+=4;
                    }
                }
                break;
            case 4:
                for (int i = 0; i<img_h; i++){
                    for (int j=0; j<realw; j+=2){
                        for (int z=0; z<2; z++){
                            if (p1[z]!=0){
                                if (w<j+z+1){
                                    w = j+z+1;
                                }
                            }
                        }
                        *p2 = p1[0]|(p1[1]<<4);
                        p2++;
                        p1+=2;
                    }
                }
                break;
            }
            if (is_output_width){
                if (w == 0){
                    w = img_w;
                }
                *cwidth = w;
            }
        }

        fbin.writeRawData((const char*)out, out_size);
        delete[] out;
        if (remain>128){
            remain = remain-128;
            rcount = 128;
        }else{
            rcount = remain;
            remain = 0;
        }
    }
    text<<"start = "<<start<<", ";
    text<<"count = "<<count<<endl;
    is_creating = false;
}
